#include "Log.h"
#include <exception>
#include <stdexcept>
#include <string>

Log g_DefaultLog;

const std::string	Log::m_fnGetLevelString(unsigned char fLevel)
{
	if((fLevel & All)==All)
		return "all";

	std::string result;

	if(fLevel & Hint)
		result = "hint + ";
	if(fLevel & Success)
		result += "success + ";
	if(fLevel & Warning)
		result += "warning + ";
	if(fLevel & Error)
		result += "error + ";
	if(fLevel & FatalError)
		result += "fatal error + ";

	if(result!="")
		result.resize(result.length()-3);
	else
		result="none";
	return result;
}

Log::Log(const char *szFileName)
{
#ifdef _DEBUG
	m_LogFile.open(szFileName, std::ios::out|std::ios::app);
	if(! m_LogFile.is_open())return;

	m_LogFile<<"<hr>\n";
	SetOuputLevels(All);
	LOG_PRINT("Log-file was opened for append information.", Hint);
#endif
}

Log::~Log(void)
{
#ifdef _DEBUG
	LOG_PRINT("Closing log file.", Hint);
	m_LogFile<<"<hr>\n";
	m_LogFile.close();
#endif
}


void Log::SetOuputLevels(const unsigned char fLevels)
{
#ifdef _DEBUG
	if(fLevels>All)
		throw std::runtime_error("Output level of log-file is invalid!");

	m_fLevels = fLevels;
	LOG_PRINT(std::string("Log's output level is now changed to: "+m_fnGetLevelString(m_fLevels)+"."), Hint);
#endif	
}

